package com.wiz.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;


/**
 * 请求处理
 * @author Hakeny
 */
public class RequestInterceptor extends HandlerInterceptorAdapter{

	private transient final Logger log = LoggerFactory.getLogger(getClass());
	
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handle) {
        String requestURI=request.getRequestURI();
        requestURI = requestURI==null ? "":requestURI;
//        System.out.println(requestURI+" from "+ request.getLocalAddr());
        try {
            //出现了乱码问题
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
//            response.setHeader("Access-Control-Allow-Origin", "*");//配合网页测试，上成产前必须删除
            return true;
        } catch(Exception e) {
        	e.printStackTrace();
            log.error("拦截器异常："+e,e);
            return false;
        }
    }
    
    @Override
    public void postHandle(HttpServletRequest request,
    		HttpServletResponse response, Object handler,
    		ModelAndView modelAndView) throws Exception {
    	super.postHandle(request, response, handler, modelAndView);
    }
    
    @Override
    public void afterCompletion(HttpServletRequest request,
    		HttpServletResponse response, Object handler, Exception ex)
    		throws Exception {
    	super.afterCompletion(request, response, handler, ex);
    }
    
}
